package com.frogs.ucenter.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.DWZResponse;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.RenderUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.ucenter.entity.WechatUserPhone;
import com.frogs.ucenter.service.WechatUserPhoneService;

@Controller
@RequestMapping("/admin/wechatUserPhone")
public class WechatUserPhoneController extends BaseController3 {
    private static final String ROOT_VIEW = "admin/wechatUserPhone";

    private static final String HOME_VIEW = "admin/wechatUserPhone/home";

    private static final String EDIT_VIEW = "admin/wechatUserPhone/edit";

    @Autowired
    private WechatUserPhoneService wechatUserPhoneService;

    @RequestMapping()
    public String listWechatUserPhone(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, true );
        getPageList( model );
        return HOME_VIEW;
    }

    @RequestMapping(EDIT)
    public String createWechatUserPhone(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, false );
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.put( "model",wechatUserPhoneService.getById( id ) ); 
        }
        return EDIT_VIEW;
    }

    @RequestMapping(ADD)
    public String saveWechatUserPhone(HttpServletRequest request, HttpServletResponse response, ModelMap model, WechatUserPhone wechatUserPhone) {
        DWZResponse.Builder builder;
        try {
            wechatUserPhoneService.insert( wechatUserPhone ); 
            	builder = DWZResponse.getSucessBuilder("�����ɹ�");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("����ʧ�ܣ�" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        return null;
    }

    @RequestMapping(SHOW)
    public String showWechatUserPhone(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.addAttribute( wechatUserPhoneService.getById( id ) ); 
        }
        setRequestModelMap(request, model);
        return null;
    }

    @RequestMapping(UPDATE)
    public String updateWechatUserPhone(HttpServletRequest request, HttpServletResponse response, ModelMap model, WechatUserPhone wechatUserPhoneNew) throws Exception {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            DWZResponse.Builder builder;
            try {
                WechatUserPhone wechatUserPhone = wechatUserPhoneService.getById( id ); 
                PropertyUtils.copyProperties(wechatUserPhone, wechatUserPhoneNew);
                wechatUserPhoneService.update( wechatUserPhone ); 
                model.clear();
                builder = DWZResponse.getSucessBuilder("���³ɹ�");
            } catch (Exception e) {
                builder = DWZResponse.getFailBuilder("����ʧ�ܣ�" + e.getMessage());
            }
            RenderUtil.renderHtml(builder.build().toString(), response);
        }
        return null;
    }

    @RequestMapping(DELETE)
    public String deleteWechatUserPhone(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap(request, model);
        DWZResponse.Builder builder;
        try {
                if (model.get("inIdList") != null) {
                wechatUserPhoneService.delete( model ); 
                    }
                    builder = DWZResponse.getSucessBuilder("ɾ��ɹ�");
            } catch (Exception e) {
                    builder = DWZResponse.getFailBuilder("ɾ��ʧ�ܣ�" + e.getMessage());
            }
            finally {model.clear();}
            RenderUtil.renderHtml(builder.build().toString(), response);
            return null;
        }

    private PageList<WechatUserPhone> getPageList(ModelMap model) {
        PageList<WechatUserPhone> wechatUserPhoneList = wechatUserPhoneService.getList( model, Integer.parseInt(model.get("pageNum").toString()), Integer.parseInt(model.get("numPerPage").toString()) ); 
        model.put("wechatUserPhoneList",wechatUserPhoneList);
        model.put("pageTurn",wechatUserPhoneList.getPageTurn());
        return wechatUserPhoneList;
    }
}